package com.example.qxfw.ybzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.MapPoint;
import com.example.qxfw.common.entity.qx.TownshipForecast;
import com.example.qxfw.common.util.DmgisUtils;
import com.example.qxfw.ybzz.mapper.TownshipForecastMapper;
import com.example.qxfw.ybzz.service.TownshipForecastService;
import com.example.qxfw.yjqx.dto.RegionFcstDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

@Service
public class TownshipForecastServiceImpl implements TownshipForecastService {

    @Autowired
    private TownshipForecastMapper townshipForecastMapper;

    @Override
    public List<TownshipForecast> getList(String forecastTime, String strTimeChar, String stationId) {
        return townshipForecastMapper.selectForecast1(forecastTime, strTimeChar, stationId);
    }

    @Override
    public Integer update(TownshipForecast townshipForecast) {
        LambdaUpdateWrapper<TownshipForecast> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TownshipForecast::getStationid,townshipForecast.getStationid())
                .eq(TownshipForecast::getTimechar,townshipForecast.getTimechar())
                .eq(TownshipForecast::getNtimes,townshipForecast.getNtimes())
                .eq(TownshipForecast::getDatechar,townshipForecast.getDatechar());
        int rows  = townshipForecastMapper.update(townshipForecast,updateWrapper);
        return rows;
    }

    @Override
    public Integer delete(String dateChar, String strTimeChar, String stationList) {
        int rows = townshipForecastMapper.delete(dateChar,strTimeChar,stationList);
        return rows;
    }

    @Override
    public Integer save(TownshipForecast townshipForecast) {
        int rows = townshipForecastMapper.insert(townshipForecast);
        return rows;
    }

    @Override
    public List<TownshipForecast> getMaxTime() {
        return townshipForecastMapper.selectMaxTime();
    }

    @Override
    public List<TownshipForecast> getList(String dateChar, String timeChar, Integer nTimes1, Integer nTimes2, String city) {
        return townshipForecastMapper.selectForecast(Timestamp.valueOf(dateChar), timeChar, nTimes1, nTimes2, city);
    }

    @Override
    public List<TownshipForecast> listByParam1(String dateChar,String nTimes, String stationId, String colum, String maxTemp, String minTemp) {
        return townshipForecastMapper.selectList(Wrappers.<TownshipForecast>query()
                .select(colum+" as maxtemp")
                        .lt(StringUtils.isNotBlank(maxTemp),colum,maxTemp)
                        .gt(StringUtils.isNotBlank(minTemp),colum,minTemp)
                .lambda()
                .eq(TownshipForecast::getDatechar,dateChar)
                .eq(TownshipForecast::getNtimes,nTimes)
                .eq(TownshipForecast::getStationid,stationId));
    }

    @Override
    public List<TownshipForecast> listByParam2(String dateChar, String nTimes, String stationId, String colum, String maxTemp, String minTemp) {
        return townshipForecastMapper.selectList(Wrappers.<TownshipForecast>query()
                .select(colum+" as humid")
                .lt(StringUtils.isNotBlank(maxTemp),colum,maxTemp)
                .gt(StringUtils.isNotBlank(minTemp),colum,minTemp)
                .lambda()
                .eq(TownshipForecast::getDatechar,dateChar)
                .eq(TownshipForecast::getNtimes,nTimes)
                .eq(TownshipForecast::getStationid,stationId)
                .having(StringUtils.isNotBlank(maxTemp),colum+"<"+maxTemp));
    }


    @Override
    public List<TownshipForecast> getList3(String dateChar, String stationId, String minTemp, String maxTemp) {
        return townshipForecastMapper.selectList2(dateChar, stationId, minTemp, maxTemp);
    }

    @Override
    public List<TownshipForecast> getList4(String dateChar, String stationId, String minTemp) {
        return townshipForecastMapper.selectList3(dateChar, stationId, minTemp);
    }

    @Override
    public List<TownshipForecast> getList5(String dateChar, String stationId, String maxTemp) {
        return townshipForecastMapper.selectList4(dateChar, stationId, maxTemp);
    }

    @Override
    public List<TownshipForecast> getList6(String dateChar, String stationId, String minTemp, String maxTemp) {
        return townshipForecastMapper.selectList5(dateChar, stationId, minTemp, maxTemp);
    }

    @Override
    public List<TownshipForecast> getList7(String dateChar, String timeChar, String nTimes) {
        return townshipForecastMapper.selectList(Wrappers.<TownshipForecast>lambdaQuery()
                .eq(TownshipForecast::getDatechar,Timestamp.valueOf(dateChar))
                .eq(TownshipForecast::getTimechar,timeChar)
                .eq(TownshipForecast::getNtimes,Integer.parseInt(nTimes)));
    }

    @Override
    public List<TownshipForecast> getList8(String date, String time, String nTimes1, String nTimes2) {
        return townshipForecastMapper.selectList6(Timestamp.valueOf(date), time, nTimes1, nTimes2);
    }

    @Override
    public List<TownshipForecast> getList9(String date, String time, String nTimes1, String nTimes2,String stationId) {
        return townshipForecastMapper.selectList7(Timestamp.valueOf(date), time, nTimes1, nTimes2, stationId);
    }

    @Override
    public List<TownshipForecast> getList10(String date, String time, String nTimes1, String nTimes2) {
        return townshipForecastMapper.selectList8(Timestamp.valueOf(date),time,nTimes1,nTimes2);
    }

    @Override
    public List<TownshipForecast> listByParam(String dateChar, String timeChar, Integer nTimes1, Integer nTimes2, String stationId) {
        return townshipForecastMapper.selectList(Wrappers.<TownshipForecast>query().select("stationid", "stationname", "Longitude", "Latitude", "rain", "Weather1", "CAST(cast(WindS AS VARCHAR) as numeric(18,1)) as WindS", "CAST(cast(WindD AS VARCHAR) as numeric(18,1)) as WindD", "CAST(cast(Humid AS VARCHAR) as numeric(18,1)) as Humid,maxtemp,mintemp,cloud,cloudlow ").lambda()
                .eq(TownshipForecast::getStationid,stationId)
                .eq(TownshipForecast::getDatechar, Timestamp.valueOf(dateChar))
                .eq(TownshipForecast::getTimechar,timeChar)
                .and(w->w
                        .eq(TownshipForecast::getNtimes,nTimes1)
                        .or()
                        .eq(TownshipForecast::getNtimes,nTimes2)
                ).orderByDesc(TownshipForecast::getStationid,TownshipForecast::getNtimes));
    }

    @Override
    public List<String> getRain(String dateChar, String timeChar, String nTimes, String rain, String stationId) {
        return townshipForecastMapper.selectRain(dateChar, timeChar, nTimes, rain, stationId);
    }

    @Override
    public List<String> getTemp(String dateChar, String timeChar, String nTimes, String temp, String stationId) {
        return townshipForecastMapper.selectTemp(dateChar, timeChar, nTimes, temp, stationId);
    }

    @Override
    public List<String> getWindS(String dateChar, String timeChar, String nTimes, String winds, String stationId) {
        return townshipForecastMapper.selectWindS(dateChar, timeChar, nTimes, winds, stationId);
    }

    @Override
    public List<String> getPress(String dateChar, String timeChar, String nTimes, String press, String stationId) {
        return townshipForecastMapper.selectPress(dateChar, timeChar, nTimes, press, stationId);
    }

    @Override
    public List<String> getHumid(String dateChar, String timeChar, String nTimes, String humid, String stationId) {
        return townshipForecastMapper.selectHumid(dateChar, timeChar, nTimes, humid, stationId);
    }

    @Override
    public List<String> getDateChar() {
        return townshipForecastMapper.selectDateChar();
    }

    @Override
    public List<TownshipForecast> listByRegion(RegionFcstDto regionFcstDto) {
        System.out.println(regionFcstDto);
        List<TownshipForecast> townshipForecasts;
        if (ObjectUtils.isNotEmpty(regionFcstDto.getRadius())&&regionFcstDto.getRadius()!=0){
            townshipForecasts = townshipForecastMapper.selectList(Wrappers.<TownshipForecast>query()
                    .select( "StationID", "StationName", "Longitude", "Latitude", "Weather1", "MaxTemp", "MinTemp", "WindS", "WindD", "AreaName")
                    .eq("to_char(DateChar,'yyyy-MM-dd')",regionFcstDto.getDateChar())
                    .eq("TimeChar",regionFcstDto.getTimeChar())
                    .eq("NTimes",regionFcstDto.getNtimes())
                    .apply( "( 6371000 * acos ( " +
                            " cos( radians( {1}))" +
                            " * cos( radians( CAST(Latitude as numeric)))" +
                            " * cos( radians( CAST(Longitude as numeric)) - radians( {0}))" +
                            " + sin( radians( {1} ))" +
                            " * sin( radians( CAST(Latitude as numeric)))" +
                            ")) < {2} ",regionFcstDto.getLon(),regionFcstDto.getLat(),regionFcstDto.getRadius()));
        } else {
            townshipForecasts = townshipForecastMapper.selectList(Wrappers.<TownshipForecast>query()
                    .select( "StationID", "StationName", "Longitude", "Latitude", "Weather1", "MaxTemp", "MinTemp", "WindS", "WindD", "AreaName")
                    .eq("to_char(DateChar,'yyyy-MM-dd')",regionFcstDto.getDateChar())
                    .eq("TimeChar",regionFcstDto.getTimeChar())
                    .eq("NTimes",regionFcstDto.getNtimes()));
            Iterator<TownshipForecast> iterator = townshipForecasts.iterator();
            while (iterator.hasNext()){
                TownshipForecast next = iterator.next();
                MapPoint mapPoint = new MapPoint(next.getLongitude(),next.getLatitude());
                if (!DmgisUtils.isScope(mapPoint,regionFcstDto.getPos())){
                    iterator.remove();
                }
            }
        }
        return townshipForecasts;
    }

    @Override
    public List<Double> getMaxTemp(String dateChar, String nTimes, String timeChar, String stationId) {
        return townshipForecastMapper.selectMaxTemp(dateChar, nTimes, timeChar, stationId);
    }

    @Override
    public List<Double> getAvgHumid(String dateChar, String nTimes, String timeChar, String stationId, Double minHumid, Double maxHumid) {
        return townshipForecastMapper.selectAvgHumid(dateChar, nTimes, timeChar, stationId, minHumid, maxHumid);
    }
}
